package upload

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func UploadFile(c *gin.Context) {

	file, err := c.FormFile("file")

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    -1,
			"message": err.Error(),
			"data":    "",
		})
		return
	}
	err1 := c.SaveUploadedFile(file, "./upload/"+file.Filename)
	if err1 != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    -1,
			"message": err1.Error(),
			"data":    "",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    1,
		"message": "上传成功！",
		"data":    file.Filename,
	})
}

func UploadFiles(c *gin.Context) {

	form, _ := c.MultipartForm()
	files := form.File["files"]
	length := len(files)
	if length <= 0 {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    -1,
			"message": "上传文件不能为空!",
			"data":    "",
		})
		return
	}
	list := make([]string, length)
	for i, file := range files {
		c.SaveUploadedFile(file, "./upload/"+file.Filename)
		list[i] = file.Filename
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    1,
		"message": "上传成功！",
		"data":    list,
	})

}
